
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评论管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索："> </el-form-item>
      <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.comment_id"
          placeholder="输入id"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="comment_id"
        label="评论id"
        width="120"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="user_id"
        label="用户id"
        width="120"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="blog_id"
        label="博客id"
        width="120"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="comment_date"
        label="评论时间"
        width="120"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="comment_content"
        label="评论内容"
        min-width="50"
      >
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteUser(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%">
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="评论id" prop="comment_id">
          <el-input
            size="small"
            v-model="editForm.comment_id"
            auto-complete="off"
            placeholder="用户id"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户id" prop="user_id">
          <el-input
            size="small"
            v-model="editForm.user_id"
            auto-complete="off"
            placeholder="请输入用户id"
          ></el-input>
        </el-form-item>
        <el-form-item label="博客id" prop="blog_id">
          <el-input
            size="small"
            v-model="editForm.blog_id"
            auto-complete="off"
            placeholder="请输入博客id"
          ></el-input>
        </el-form-item>
        <el-form-item label="评论时间" prop="comment_date">
          <el-input
            size="small"
            v-model="editForm.comment_date"
            auto-complete="off"
            placeholder="请输入评论时间"
          ></el-input>
        </el-form-item>
        <el-form-item label="评论内容" prop="comment_content">
          <el-input
            size="small"
            v-model="editForm.comment_content"
            auto-complete="off"
            placeholder="请输入评论内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="updateForm('editForm')"
          >更新</el-button
        >
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="addForm('editForm')"
          >添加</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { commentall, select, del, add, update } from "../../api/commentapi";
import { jiancomm } from "../../api/blogapi";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: "添加用户",
      editFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      // 编辑与添加
      editForm: {
        comment_id: "",
        user_id: "",
        blog_id: "",
        comment_date: "",
        comment_content: "",
      },
      rules: {
        deptName: [
          { required: true, message: "请输入评论id", trigger: "blur" },
        ],
        deptNo: [{ required: true, message: "请输入评论id", trigger: "blur" }],
      },
      // 选择数据
      selectdata: [],
      formInline: {
        page: 1,
        limit: 10,
        deptId: "",
        comment_id: "",
        userMobile: "",
        isLock: "",
      },
      //用户数据
      listData: [],
      // 数据权限
      UserDept: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      // 选中
      checkmenu: [],
      //参数role
      saveroleId: "",
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  watch: {},

  created() {
    this.getdata(this.formInline);
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true;
      // 模拟数据开始
      let res = {
        code: 0,
        msg: null,
        count: 12,
      };
      this.loading = false;
      this.userData = res.data;
      // 分页赋值
      this.pageparm.currentPage = this.formInline.page;
      this.pageparm.pageSize = this.formInline.limit;
      this.pageparm.total = res.count;
      commentall()
        .then((res) => {
          this.loading = false;
          if (res.success == 404) {
            this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            this.listData = res;
            for (var i = 0; i <= this.listData.length; i++) {
              this.listData[i].comment_date = this.GetDateFormat(
                this.listData[i].comment_date
              );
            }
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = res.count;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message({
            type: "success",
            message: "获取评论成功！",
          });
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getdata(this.formInline);
    },
    GetDateFormat(str) {
      return new Date(parseInt(str.substr(6, 13))).toLocaleDateString();
    },
    //搜索事件
    search() {
      select(this.formInline.comment_id).then((res) => {
        if (res == 404) {
          this.$message({
            type: "error",
            message: "查询的用户id不存在",
          });
        } else {
          this.listData.splice(res);
          this.listData.push(res);
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page;
          this.pageparm.pageSize = this.formInline.limit;
          this.pageparm.total = res.count;
        }
      });
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改用户";
        this.editForm.comment_id = row.comment_id;
        this.editForm.user_id = row.user_id;
        this.editForm.blog_id = row.blog_id;
        this.editForm.comment_date = row.comment_date;
        this.editForm.comment_content = row.comment_content;
      } else {
        this.title = "添加用户";
        this.editForm.comment_id = "";
        this.editForm.user_id = "";
        this.editForm.blog_id = "";
        this.editForm.comment_date = "";
        this.editForm.comment_content = "";
      }
    },
    addForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          add(this.editForm)
            .then((res) => {
              this.editFormVisible = false;
              this.loading = false;
              if (res == 200) {
                this.getdata(this.formInline);
                this.$message({
                  type: "success",
                  message: "保存成功！",
                });
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              this.$message.error("保存失败，请稍后再试！");
            });
        } else {
          return false;
        }
      });
    },
    // 编辑、添加提交方法
    updateForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          console.log(this.editForm);
          update(this.editForm)
            .then((res) => {
              this.editFormVisible = false;
              this.loading = false;
              if (res == 200) {
                this.getdata(this.formInline);
                this.$message({
                  type: "success",
                  message: "保存成功！",
                });
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              this.$message.error("保存失败，请稍后再试！");
            });
        } else {
          return false;
        }
      });
    },

    // 删除用户
    deleteUser(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del(row.comment_id)
            .then((res) => {
              if (res == 200) {
                console.log(row.blog_id);
                jiancomm(row.blog_id).then((res) => {
                  this.$message({
                    type: "success",
                    message: "评论已删除!",
                  });
                  this.getdata(this.formInline);
                });
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    changemenu(arr) {
      let change = [];
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
          change.push(arr[i].id);
        }
      }
      return change;
    },
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 